//
//  ManagerData.m
//  NhomThuy
//
//  Created by HOCVIEN on 1/27/15.
//  Copyright (c) 2015 t3h. All rights reserved.
//

#import "ManagerData.h"
static ManagerData* instance;
@implementation ManagerData
+ (ManagerData*)defaultManager{
    if(!instance){
        instance = [[ManagerData alloc]init];
        instance.vnSongList = [[NSMutableArray alloc]init];
        instance.enSongList = [[NSMutableArray alloc]init];
        instance.singerList = [[NSMutableArray alloc]init];
        instance.loveSongList = [[NSMutableArray alloc]init];
        [instance loadData];
    }
    return instance;
}
-(void)loadData{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"data" ofType:@"plist"];
    NSDictionary *file = [NSDictionary dictionaryWithContentsOfFile:path];
    self.vnSongList = [file objectForKey:@"vi-songs"];
    self.enSongList = [file objectForKey:@"en-songs"];
    self.singerList = [file objectForKey:@"singers"];
    
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"lovesong.plist"];
    BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:docPath];
    if(!exists){
        path = [[NSBundle mainBundle]pathForResource:@"lovesong" ofType:@"plist"];
        file = [NSDictionary dictionaryWithContentsOfFile:path];
        [file writeToFile:docPath atomically:YES];
    }else{
        file = [NSDictionary dictionaryWithContentsOfFile:path];
    }
    self.loveSongList = [file objectForKey:@"love-songs"];
}
- (void)addLoveSongWithDictionaryOfSong:(NSDictionary *)song{
    [self.loveSongList addObject:song];
    NSDictionary *file = [[NSDictionary alloc]initWithObjectsAndKeys:self.loveSongList,@"love-songs", nil];
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"lovesong.plist"];
    [file writeToFile:docPath atomically:YES];
}
- (void)removeLoveSongWithDictionaryOfSong:(NSDictionary *)song{
    [self.loveSongList removeObject:song];
    NSDictionary *file = [[NSDictionary alloc]initWithObjectsAndKeys:self.loveSongList,@"love-songs", nil];
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"lovesong.plist"];
    [file writeToFile:docPath atomically:YES];
}
- (NSMutableArray *)getSongListBySingerID:(NSString*)singerID{
    NSMutableArray *array = [[NSMutableArray alloc]init];
    for (NSDictionary *song in self.vnSongList) {
        if ([[song objectForKey:@"singer-id"] isEqualToString:singerID]) {
            [array addObject:song];
        }
    }
    for (NSDictionary *song in self.enSongList) {
        if ([[song objectForKey:@"singer-id"] isEqualToString:singerID]) {
            [array addObject:song];
        }
    }
    return array;
}
- (UIColor *)setBackgroundColorForNavi{
    return [UIColor colorWithRed:168/255.f green:82/255.f blue:32/255.f alpha:1];
}
- (UIColor *)setTintColorForNavi{
    return [UIColor whiteColor];
}
- (UIView *)setImageTitleForNavi{
    UIImage* myImage = [UIImage imageNamed:@"img_logo.png"];
    UIImageView* imageView = [[UIImageView alloc] initWithImage:myImage];
    return imageView;
}
@end
